﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Whips
{
    public class HungryGhost : ModProjectile
    {
        private int npcTargetIndex = -1;
        private int attackTimer = 0;
        private readonly int lifeTime = 300;
        private Vector2 biteOffset = Vector2.Zero;
        private bool hasInitialized = false;
        private bool isFlipped = false;

        public override void SetStaticDefaults()
        {
            Main.projFrames[Type] = 6;
        }

        public override void SetDefaults()
        {
            Projectile.width = 32;
            Projectile.height = 32;
            Projectile.friendly = true;
            Projectile.hostile = false;
            Projectile.penetrate = -1;
            Projectile.timeLeft = lifeTime;
            Projectile.tileCollide = false;
            Projectile.ignoreWater = true;
            Projectile.DamageType = DamageClass.Summon;
            Projectile.netImportant = true;
        }

        public override void AI()
        {
            if (!hasInitialized && Projectile.ai[0] >= 0 && Projectile.ai[0] < Main.npc.Length)
            {
                npcTargetIndex = (int)Projectile.ai[0];
                hasInitialized = true;
                float angle = Main.rand.NextFloat(-MathHelper.PiOver2, MathHelper.PiOver2);
                biteOffset = new Vector2(
                    (Main.npc[npcTargetIndex].width / 2 + 4) * (Main.rand.NextBool() ? 1 : -1),
                    Main.rand.NextFloat(-Main.npc[npcTargetIndex].height / 5, Main.npc[npcTargetIndex].height / 5)
                );
                isFlipped = biteOffset.X < 0;
                for (int i = 0; i < 15; i++)
                {
                    Dust dust = Dust.NewDustDirect(
                        Projectile.position,
                        Projectile.width,
                        Projectile.height,
                        DustID.Blood,
                        Main.rand.NextFloat(-2f, 2f),
                        Main.rand.NextFloat(-2f, 2f),
                        0,
                        default,
                        1.4f
                    );
                    dust.noGravity = true;
                }
                SoundEngine.PlaySound(SoundID.NPCDeath19, Projectile.Center);
            }
            if (npcTargetIndex < 0 || npcTargetIndex >= Main.npc.Length || !Main.npc[npcTargetIndex].active || !Main.npc[npcTargetIndex].CanBeChasedBy())
            {
                Projectile.Kill();
                return;
            }
            NPC target = Main.npc[npcTargetIndex];
            Projectile.Center = target.Center + biteOffset;
            Projectile.frameCounter++;
            if (Projectile.frameCounter >= 4)
            {
                Projectile.frameCounter = 0;
                Projectile.frame = (Projectile.frame + 1) % Main.projFrames[Type];
                if (Projectile.frame == 2 || Projectile.frame == 5)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        Dust dust = Dust.NewDustDirect(
                            Projectile.position,
                            Projectile.width,
                            Projectile.height,
                            DustID.Blood,
                            Main.rand.NextFloat(-1f, 1f),
                            Main.rand.NextFloat(-1f, 1f),
                            0,
                            default,
                            1.2f
                        );
                        dust.noGravity = true;
                    }
                }
            }
            if (++attackTimer >= 30)
            {
                attackTimer = 0;

                int damage = (int)(Projectile.damage * 0.3f);
                target.SimpleStrikeNPC(damage, 0);
                for (int i = 0; i < 6; i++)
                {
                    Dust dust = Dust.NewDustDirect(
                        Projectile.position,
                        Projectile.width,
                        Projectile.height,
                        DustID.Blood,
                        Main.rand.NextFloat(-1.5f, 1.5f),
                        Main.rand.NextFloat(-1.5f, 1.5f),
                        0,
                        default,
                        1.3f
                    );
                    dust.noGravity = true;
                }

                SoundEngine.PlaySound(SoundID.NPCDeath19, Projectile.Center);
            }
            Lighting.AddLight(Projectile.Center, new Vector3(0.8f, 0.1f, 0.1f) * 0.6f);
        }

        public override bool? Colliding(Rectangle projHitbox, Rectangle targetHitbox)
        {
            return false;
        }

        public override bool PreDraw(ref Color lightColor)
        {
            Texture2D texture = TextureAssets.Projectile[Type].Value;
            Rectangle frame = texture.Frame(1, Main.projFrames[Type], 0, Projectile.frame);
            Vector2 origin = new(frame.Width * 0.5f, frame.Height * 0.7f);
            SpriteEffects spriteEffects = isFlipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
            Main.EntitySpriteDraw(
                texture,
                Projectile.Center - Main.screenPosition,
                frame,
                Color.White,
                0f,
                origin,
                Projectile.scale,
                spriteEffects,
                0
            );
            Main.EntitySpriteDraw(
                texture,
                Projectile.Center - Main.screenPosition,
                frame,
                new Color(255, 100, 100, 0) * 0.4f,
                0f,
                origin,
                Projectile.scale * 1.1f,
                spriteEffects,
                0
            );

            return false;
        }
    }
}
